import csv
import random
import pandas as pd
class quiz():
    def __init__(self):
        ## 雅思词汇答对个数
        self.ieltsCounter = 0
        ## 四六级词汇答对个数
        self.cetCounter = 0
        ## 考研词汇答对个数
        self.bstsCounter = 0
    
        ## 雅思权重
        self.weightIelts = 0.6
        ## 四六级权重
        self.weightcet = 0.25
        ## 考研权重
        self.weightBsts = 0.15
        self.path = "/Estimation-vocabulary/code/keshe/backServer/tool/Quiz/"
        
        ieltsNum = 1
        cetsNum = 1
        bstsNum = 1
    ## 读取每个词库的csv文件
    def GetWordcsv(self):
        # 读取雅思词汇
        wordList1 = pd.read_csv(self.path + 'ielts1.csv')['word'].tolist()
        # 读取四六级词汇
        wordList2 = pd.read_csv(self.path + 'cet1.csv')['word'].tolist()
        # 读取考研词汇
        wordList3 = pd.read_csv(self.path + 'bsts1.csv')['word'].tolist() 
        for i in range(len(wordList1) - 1, -1, -1):
            if wordList1[i] in wordList2:
                del wordList1[i]
                
        for i in range(len(wordList3) - 1, -1, -1):
            if wordList3[i] in wordList2:
                del wordList3[i]
                
        return wordList1,wordList2,wordList3

    def Problem(self, wordList1, wordList2, wordList3,problemNum=100):
        ## 出题
        problem = [{'word':None,'flag':False} for i in range(problemNum)]
        ## 每一个题库抽题数
        quiz.ieltsNum = int(problemNum*self.weightIelts)
        quiz.cetsNum = int(problemNum*self.weightcet)
        quiz.bstsNum = int(problemNum*self.weightBsts)
        print("Problem:", quiz.ieltsNum, quiz.cetsNum, quiz.bstsNum)
        problemList = []
        
        quiz.ieltsNum = int(problemNum * (len(wordList1)/(len(wordList1) + len(wordList2) + len(wordList3))))
        ieltsList = random.sample(wordList1, quiz.ieltsNum)
        problemList = problemList + ieltsList
        
        quiz.cetsNum = int(problemNum * (len(wordList2)/(len(wordList1) + len(wordList2) + len(wordList3))))
        cetsList = random.sample(wordList2, quiz.cetsNum)
        problemList = problemList + cetsList
        
        quiz.bstsNum = problemNum - quiz.ieltsNum - quiz.cetsNum
        bstsList = random.sample(wordList3, quiz.bstsNum)
        problemList = problemList + bstsList
                       
        random.shuffle(problemList)
        for i in range(len(problemList)):
            problem[i]['word'] = problemList[i]
        return problem


    ## 统计答卷情况
    def getResult(self,answer,wordList1,wordList2,wordList3):
        ieltsrightCount = 0
        cetrightCount = 0
        bstrightCount = 0
        
        if answer and wordList1 and wordList2 and wordList3:
            for i in range(len(answer)):
                if answer[i]:
                    strTemp = str(answer[i]['word'])
                    if answer[i]['flag'] == True and strTemp in wordList1:
                        ieltsrightCount += 1
                    elif answer[i]['flag'] == True and strTemp in wordList2:
                        cetrightCount += 1
                    elif answer[i]['flag'] == True and strTemp in wordList3:
                        bstrightCount += 1
                    else:
                        continue
        return ieltsrightCount, cetrightCount, bstrightCount

    ## 分析答卷结果（预估词汇量）
    def evaluateResult(self,result,wordList1,wordList2,wordList3):
        ieltsrightCount, cetrightCount, bstrightCount = result
        ieltRightRate = ieltsrightCount / quiz.ieltsNum
        print(ieltsrightCount,'  ',ieltRightRate, '  ', quiz.ieltsNum)
        cetRightRate = cetrightCount / quiz.cetsNum
        print(cetrightCount,'  ', cetRightRate, '  ', quiz.cetsNum)
        bstRightRate = bstrightCount / quiz.bstsNum
        print(bstrightCount,'  ', bstRightRate, '  ', quiz.bstsNum)
        totalRate = 0
        totalRate = ieltRightRate * self.weightIelts + cetRightRate * self.weightcet \
        + bstRightRate * self.weightBsts
        
        total_len = int(len(wordList1) + len(wordList2) + len(wordList3))
        print("len of words:",total_len)
        total = int(total_len * totalRate)
        return total
    
    

